// pages/user/money.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:3,
    showAddBank: 0,
    withDrawList:[],
    page1: 1,
    account:'',
    nodata: 0,
    nodata2: 0,
    name:'',
    lastpage1: 0,
    user_info:{},
    array: ['招商银行', '北京银行'],
    bankname: '',
    selectBank:{},
    page:1,
    lastpage:0,
    loading:0,
    moneyLog:[],
    freeze_money:0,
    money:0,
    myBanks:[],
    payList:[],
    listType:1,
    payListSelect:[],
    payListSelectKey:0,
    payListSelectText:'请选择',
  },
  bindChange: function (e) {
    this.data.payListSelectKey = e.detail.value;
    this.setData({ payListSelectText: this.data.payListSelect[e.detail.value] });
    console.log(e);
  },

  setStatus: function (e) {
    var listType = e.currentTarget.dataset.listtype;
    
    this.setData({ listType: listType });
    if (this.data.listType==2){
      this.getWithDrawList(2);
    }
    //console.log(e);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var user_info = wx.getStorageSync('user_info');
    // this.setData({ user_info: user_info});
    // //console.log(options);
    this.setData({ extConfig: app.getExtConfigSync() })
    this.getInfo();
    this.loadmoney();
    var account = wx.getStorageSync('account');
    if (account){
      this.setData({ account: account })
    }
    var name = wx.getStorageSync('name');
    if (name) {
      this.setData({ name: name })
    }
    // this.attention();
    
    //充多少送多少
    // this.payList();
  },
  getInfo:function(){
    var that = this;
    app.api("api/user/info", {
      fields:"avatarUrl,user_id,last_update_time,nickname,phone"
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if(!res.data.phone){
        wx.navigateTo({
          url: '/pages/user/bindphone?page=bindphone&back=1',
        })
      }
      
      that.setData({ user_info: res.data });
    })  
  },

  //资金流水 
  loadmoney:function(reload=0){
    var that = this;
    
    if (reload==1){
      this.setData({
        loading:0,
        page:1,
        lastpage:0,
        moneyLog:[],
      });
    }
    
    if (this.data.loading==1 || this.data.lastpage==1){
      return ;
    }
    var page = this.data.page;
    this.data.loading=1;
    app.api("api/finance/get", {
      page: page,
      limit:10,
      order:'id desc'
    },function(res){
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        that.data.lastpage=1;
      }
      that.data.page++;
      that.setData({
        moneyLog: that.data.moneyLog.concat(res.data.list),
        freeze_money: res.data.freeze_money,
        money: res.data.money
      });
      var nodata = 0;
      if (that.data.moneyLog.length==0){
        nodata = 1;
      }
      that.setData({ nodata: nodata });
    })
  },
  showView:function(e){
    this.setViweType(e.currentTarget.dataset.id );
    console.log(e.currentTarget.dataset.id);
  },
  setViweType:function(id){
    this.setData({ type: id });
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  unDoWithDraw:function(){
    this.setViweType(3);
    //setViweType(3)
  },
  getWithDrawList: function (reload=0) {
    this.setViweType(2);
    this.setData({ listType:2});
    var that = this;
    if (reload == 2 && this.data.withDrawListloaded==1){
      return ;
    }
    if (reload == 1) {
      this.setData({
        loading: 0,
        page1: 1,
        lastpage1: 0,
        withDrawList: [],
      });
    }

    if (this.data.loading == 1 || this.data.lastpage1 == 1) {
      return;
    }
    this.data.withDrawListloaded = 1;
    var page = this.data.page1;
    this.data.loading = 1;
    app.api("api/money_withdraw/get", {
      page: page,
      limit: 10,
      order: 'id desc'
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage1 = 1;
      }
      that.data.page1++;
      that.setData({
        withDrawList: that.data.withDrawList.concat(res.data.list),

      });
      var nodata = 0;
      if (that.data.withDrawList.length == 0) {
        nodata = 1;
      }
      that.setData({ nodata2: nodata });
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = app.getPageData("pages/user/bindphone", "reload");
    if (reload==1){
      this.onPullDownRefresh();
       app.setPageData("pages/user/bindphone", { reload: 0 });
    }
    
  },
//充值
  doRecharge:function(e){

    var that = this;
    var postData = e.detail.value;
  
    if ( postData.name==""){
       app.alert('真实姓名不能为空');
       return ; 
    }
    if (postData.account == "") {
      app.alert('支付宝帐号不能为空');
      return;
    }

    that.setData({ loading: 1, disabled: 1 });
    that.data.payloading = 1;
    wx.setStorageSync('name', postData.name);
    wx.setStorageSync('account', postData.account);
    app.api("/api/user/withDraw", {
      name:postData.name,
      account: postData.account
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0, 'disabled': 0});
        if(res.code==3){
          that.attention();
        }
        return false;
      }
      that.getWithDrawList(1);
      that.loadmoney(1);
      that.setData({ 'loading': 0, 'disabled': 0 });
    })
  },

  //关注册公众号
  attention:function(){
    wx.previewImage({
      current: 'https://img.bestsmartbee.com/1545552903255', // 当前显示图片的http链接
      urls: ['https://img.bestsmartbee.com/1545552903255'] // 需要预览的图片http链接列表
    })
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo();
    this.loadmoney(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.listType == 1) {
      this.loadmoney();
    } else {
      this.getWithDrawList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})